
package org.apache.osgimaker.analyse.algorithm.dependency;

import java.io.PrintWriter;
import java.util.Map;

import org.apache.osgimaker.analyse.algorithm.CodeAnalyser;
import org.apache.osgimaker.analyse.algorithm.graph.AtomicVertex;


/**
 * Checks a class graph for unwanted dependencies. The dependencies are
 * described by a dependency definition file (<tt>.ddf</tt>).  
 * 
 */
public class DependencyChecker
{
  private final CodeAnalyser _analyser;
  private final ResultRenderer _renderer;
  private final DependencyProcessor _processor;
  
  /**
   * Creates a new instance.
   * Note, that the constructor does not create the graph. It only parses
   * <tt>dependencyDefinition</tt> as a preprocessing step. The calculation
   * of the graph is done in {@link #check(PrintWriter)}.
   * 
   * @param analyser Analyzer instance.
   * @param dependencyDefinition Description (as read from a .ddf file) of the
   *        dependencies to be checked.
   * @param renderer Output renderer for unwanted dependencies found.
   */
  public DependencyChecker(CodeAnalyser analyser,
                           String dependencyDefinition, Map properties,
                           ResultRenderer renderer)
  {
    _analyser = analyser;
    _renderer = renderer;
    DependencyProperties dp = new DependencyProperties(properties);
    _processor = new DependencyProcessor(dependencyDefinition, dp, renderer);
  }
  
  /**
   * Checks the graph and write unwanted dependencies onto the specified
   * writer.
   * @return <tt>true</tt> if no unwanted dependency has been found.
   */
  public boolean check(PrintWriter writer)
  {
    Result result = check();
    writer.print(_renderer.render(result));
    return result.isOk();
  }

  /**
   * Checks the graph.
   */
  public Result check()
  {
    AtomicVertex[] graph = _analyser.getClassGraph();
    ResultContainer result = new ResultContainer();
    while (_processor.hasMoreStatements())
    {
      result.add(_processor.executeNextStatement(graph));
    }
    return result;
  }
  
  /**
   * Runs the DependencyChecker application. 
   * Exit 0 if no unwanted dependency found otherwise 1 is returned.
   */
  public static void main(String[] args)
  {
    DependencyCheckerCommandLine commandLine 
        = new DependencyCheckerCommandLine(args);
    if (!commandLine.isValid()) {
      System.out.println("Usage: java -cp cycle.jar cycle.DependencyChecker "
                         + commandLine.getUsage());
      System.exit(1);
    }

    CodeAnalyser analyser = new CodeAnalyser(commandLine.getClassFiles(), 
                                     commandLine.getPattern(), 
                                     commandLine.getReflectionPattern(), 
                                     commandLine.isMergeInnerClasses());
    DependencyChecker dependencyChecker 
        = new DependencyChecker(analyser, 
                                commandLine.getDependencyDefinition(),
                                System.getProperties(),
                                commandLine.getRenderer());
    PrintWriter printWriter = new PrintWriter(System.out);
    boolean ok = dependencyChecker.check(printWriter);
    printWriter.flush();
    System.exit(ok ? 0 : 1);
  }
}
